﻿using System;
using System.Collections;
using System.Text;
using System.Data;
using System.Data.SqlClient;

namespace RestService.Processing
{
    public class AP : DoiTuong
    {
        #region string Name property
        public string Name
        {
            get
            {
                if (_DataRow["Name"] is string) return (string)_DataRow["Name"];
                return "";
            }
            set
            {
                _DataRow["Name"] = value;
            }
        }
        #endregion

        #region string MAC property
        public string MAC
        {
            get
            {
                if (_DataRow["MAC"] is string) return (string)_DataRow["MAC"];
                return "";
            }
            set
            {
                _DataRow["MAC"] = value;
            }
        }
        #endregion

        public AP(DataRow dataRow)
            : base(dataRow)
        {
            DanhMucAP dm = new DanhMucAP();
            if (this.ID <= 0) this.ID = dm.GetNextID();
        }
    }

    public class DanhMucAP : DanhMucDoiTuong, IEnumerable
    {
        #region Instance property: Singleton Design pattern
        private static DanhMucAP _Instance;
        public static DanhMucAP Instance
        {
            get
            {
                if (_Instance == null)
                {
                    _Instance = new DanhMucAP();
                    _Instance.DataView = _Instance.DataTable.DefaultView;
                }
                else _Instance.LoadFromDB();
                return _Instance;
            }
        }
        #endregion

        #region override TableName property
        public override string TableName
        {
            get
            {
                return "Ap";
            }
        }
        #endregion

        #region Methods required by IEnumerable (use for foreach)
        public IEnumerator GetEnumerator()
        {
            return new TokenEnumerator(this);
        }

        private class TokenEnumerator : IEnumerator
        {
            private int _Position = -1;
            DanhMucDoiTuong _Dstk;

            public TokenEnumerator(DanhMucDoiTuong dstk)
            {
                _Dstk = dstk;
            }

            // Declare the MoveNext method required by IEnumerator:
            public bool MoveNext()
            {
                if (_Position < _Dstk.DataView.Count - 1)
                {
                    _Position++;
                    return true;
                }
                else
                {
                    return false;
                }
            }

            // Declare the Reset method required by IEnumerator:
            public void Reset()
            {
                _Position = -1;
            }

            // Declare the Current property required by IEnumerator:
            public object Current
            {
                get
                {
                    return new AP((_Dstk.DataView[_Position].Row));
                }
            }
        }
        #endregion

        public AP this[int index]
        {
            get
            {
                if (0 <= index && index <= this.Count)
                {
                    return new AP(DataTable.Rows[index]);
                }
                else
                {
                    return null;
                }
            }
        }

        public AP NewDoiTuong()
        {
            return new AP(DataTable.NewRow());
        }

        public AP GetByProperty(string property, object value)
        {
            DataRow[] rows = GetDataRows(property, value);
            if (rows.Length > 0) return new AP(rows[0]);
            else return null;
        }

        public AP GetByID(int  objID)
        {
            return GetByProperty("ID", objID);
        }

        public AP GetByMAC(string MAC)
        {
            return GetByProperty("MAC", MAC);
        }

        public static DanhMucAP GetAll()
        {
            return Instance;
        }

        public static DanhMucAP Filter(string filterExpression)
        {
            try
            {
                DanhMucAP obj = new DanhMucAP();
                obj.DataView = new DataView(DanhMucAP.Instance.DataTable, filterExpression, "", DataViewRowState.CurrentRows);
                return obj;
            }
            catch (Exception ex)
            {
                throw new Exception(ex.Message);
            }
        }
    }
}
